//
//  YAYISettingViewController.m
//  yayi
//
//  Created by Veer on 17/4/10.
//  Copyright © 2017年 Veer. All rights reserved.
// 设置界面父类

#import "XSettingBaseViewController.h"

#import "XMeCell.h"


@interface XSettingBaseViewController () <UITableViewDataSource, UITableViewDelegate>


@end

@implementation XSettingBaseViewController


- (NSMutableArray *)sectionArray
{
    if (nil == _sectionArray) {
        _sectionArray = [NSMutableArray array];
    }
    return _sectionArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    self.tableView.sectionHeaderHeight = 10;
    self.tableView.sectionFooterHeight = 0;
    
//    if ([self.tableView respondsToSelector:@selector(contentInsetAdjustmentBehavior)]) {
//        if (@available(iOS 11.0, *)) {
//            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        } else {
//            // Fallback on earlier versions
//        }
//    }else{
//        self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
//    }
    
    self.tableView.separatorColor = RGB(0xdd, 0xdd, 0xdd);
}

//- (instancetype)initWithStyle:(UITableViewStyle)style
//{
//    return [super initWithStyle:UITableViewStyleGrouped];
//}

- (void)setupTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = ScorllViewBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    [self.view addSubview:self.tableView];
}


#pragma mark - uitabarController代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    //    view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    view.frame = CGRectMake(0, 0, kScreenWidth, 10);
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    XSettingSectionModel *sectionModel = self.sectionArray[section];
    return sectionModel.itemArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 取出数据模型
    XSettingSectionModel *sectionModel = self.sectionArray[indexPath.section];
    XSettingItemModel *itemModel = sectionModel.itemArray[indexPath.row];
    
    XMeCell *cell = [XMeCell cellWith:tableView];
    cell.model = itemModel;
    if (indexPath.row < sectionModel.itemArray.count - 1) {
        
        cell.lineType = LineTypeCenter;
    }else{
        
        cell.lineType = LineTypeTopAndBottom;
    }
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 44;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 取出数据模型
    XSettingSectionModel *sectionModel = self.sectionArray[indexPath.section];
    XSettingItemModel *itemModel = sectionModel.itemArray[indexPath.row];
    
    // 2.操作
    if (itemModel.executeOperation) {
        itemModel.executeOperation(indexPath);
    }
    
    // 3.跳转
    if ([itemModel isKindOfClass:[XSettingItemModel class]]) {

        if (itemModel.destVcClass) {
            UIViewController *destVc = [[itemModel.destVcClass alloc] init];
            [self.navigationController pushViewController:destVc animated:YES];
        }
    }
}


#pragma mark - setter && getter


@end
